var session = sessionStorage.getItem("session");
if (!session) {
	window.parent.location = "../../login.html";
}
$(function() {
	$("#testT").tree({
		url: IP + "GetParamGroupTree?callback=?",
	})
	$("#testModeList").jqGrid({
		url: IP + "GetMilParamGroupInfo?callback=?&session=" + session,
		mtype: "Get",
		datatype: "jsonp",
		colModel: [{
			label: '序号',
			sorttype: 'integer',
			name: 'id',
			align: "center",
			key: true,
			width: 50,
		   hidden:true,
		}, {
			label: '模板名称',
			name: 'modelMane',
			width: 90
		}, {
			label: '创建人',
			name: 'cPreson',
			width: 70,
			align: "center",
		}, {
			label: '创建时间',
			name: 'cTime',
			width: 70
		}, {
			label: '备注',
			name: 'meno',
			width: 70
		}],
		loadonce: true,
		viewrecords: true,
		rowNum: 15,
		autowidth: true,
		page: 1,
		rownumbers:true,
		loadtext: '正在加载数据',
		pager: "#testModeListPager",
		onSelectRow: function(rowid) {
			$("#modelName").textbox("disable");
			$.ajax({
				type: "get",
				url: IP + "GetParamModelIDS?callback=?&session" + session + "&pid=" + rowid,
				dataType: 'jsonp',
				jsonp: "callback",
				jsonpCallback: "resultHandler",
				success: function(data) {
					var nodes = $('#testT').tree('getChecked');
					for (var j = 0; j < nodes.length; j++) {
						$("#testT").tree("uncheck", nodes[j].target);
					}
					for (var i = 0; i < data.length; i++) {
						var num = data[i].paramId;
						var treeNode = $('#testT').tree("find", num);
						$("#testT").tree("check", treeNode.target);
					}
				}
			});
		}
	})
	
	var h =$("body").height();
	$("#testModeList").jqGrid("setGridHeight",h-190);
	var template2 = {
		"groupOp": "AND",
		"rules": [{
			"field": "modelMane",
			"op": "eq",
			"data": "W"
		}, {
			"field": "cPreson",
			"op": "le",
			"data": "15"
		}]
	};
	// activate the build in search with multiple option
	$('#testModeList').navGrid("#testModeListPager", {
			search: true, // show search button on the toolbar
			add: false,
			edit: false,
			del: false,
			refresh: true
		}, {}, // edit options
		{}, // add options
		{}, // delete options
		{
			multipleSearch: true,
			multipleGroup: true,
			showQuery: true,
			// set the names of the template
			tmplNames: ["模板一"],
			// set the template contents
			tmplFilters: [template2]
		} // search options - define multiple search
	);
	AddRow();
})

function AddRow() {
	$("#modelName").textbox("enable");
	$('#testModeForm').form('load', {
		id: 0,
		modelName: '',
		cPreson: sessionStorage.getItem("realName"),
		meno: '',
	});
	$("#testT").tree("reload");
	$("#testModeList").trigger("reloadGrid"); 
}

function EditRow() {
	$("#modelName").textbox("enable");
	var rowKey = $("#testModeList").jqGrid('getGridParam', "selrow");
	if (rowKey) {
		$('#testModeForm').form('load', {
			id: $("#testModeList").getCell(rowKey, "id"),
			cPreson: $("#testModeList").getCell(rowKey, "cPreson"),
			modelName: $("#testModeList").getCell(rowKey, "modelMane"),
			meno: $("#testModeList").getCell(rowKey, "meno"),
		});
	} else {
		$.messager.alert('提醒', "没有选中行", 'info');
	}
}

function getCheckedId() {
	var nodes = $('#testT').tree('getChecked');
	var s = "";
	for (var i = 0; i < nodes.length; i++) {
		if (s != '') {
			s += ','
		};
		s += nodes[i].id
	}
	//console.log(s);	 
	return s;
}

function SaveRow() {
	var cTime =getCtime();
	var grid = $("#testModeList");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	var oldvalue = $("#testModeList").getCell(rowKey, "modelMane");
	if ($('#testModeForm').form('enableValidation').form('validate')) {
		var ids = getCheckedId();
		if (ids == "") {
			$.messager.alert("提醒", "请选择参数", "info");
			return;
		}
		$.ajax({
			type: "post", // 与可以 "get"
			url: IP + "UpdateTaskProbeGroupInfo?callback=?&session=" + session,
			data: {
				id: $("#id").val(),
				cPreson: $("#cPreson").val(),
				modelMane: $("#modelName").val(),
				meno: $("#meno").val(),
				savestr: ids,
			},
			dataType: "jsonp",
			jsonp: "callback",
			jsonpCallback: "resultHandler",
			success: function(json) {
				//console.log(json);
				if (json.result = 200) {
					if ($('#id').val() == 0) {
						$("#testModeList").addRowData(json.id, {
							id: json.id,
							cPreson: $("#cPreson").val(),
							modelMane: $("#modelName").val(),
							meno: $("#meno").val(),
                             cTime:cTime,
						}, '');
						insertopertlog('任务管理', '任务模板配置', '新增', '', $("#modeName").val());
						$.messager.show({
							title: '提醒',
							msg: '角色权限增加成功',
							timeout: 1300,
							showType: 'slide',
							style: {
								right: '',
								bottom: ''
							}
						});
					} else {
						$("#testModeList").setRowData($('#id').val(), {
							id: $("#id").val(),
							cPreson: $("#cPreson").val(),
							modelMane: $("#modeName").val(),
							meno: $("#meno").val(),
						}, '');
						insertopertlog('任务管理', '任务模板配置', '修改', oldvalue, $("#modeName").val());
						$.messager.show({
							title: '提醒',
							msg: '模板修改成功',
							timeout: 1300,
							showType: 'slide',
							style: {
								right: '',
								bottom: ''
							}
						});
					}
				} else {
					$.messager.alert('保存失败', json.message, 'error');
				}
				//保存成功后，重置控件，以免用户重复提交
				AddRow();
			}
		});
	}
}

function DeleteRow() {
	var grid = $("#testModeList");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	var oldvalue = $("#testModeList").getCell(rowKey, "modelMane");
	if (rowKey) {
		$.messager.confirm('删除提醒', '你确认要删除' + $("#testModeList").getCell(rowKey, "modelMane") + '?', function(r) {
			if (r) {
				$.ajax({
					type: "post", // 与可以 "get"
					url: IP + "DeleteParamGropeModel",
					data: {
						session: session,
						id: rowKey,
					},
					dataType: "jsonp",
					jsonp: "callback",
					jsonpCallback: "resultHandler",
					success: function(json) {
						if (json.result == 200) {
							$("#testModeList").delRowData(rowKey);
							insertopertlog('任务管理', '任务模板配置', '删除', oldvalue, '');
							$.messager.show({
								title: '提醒',
								msg: '删除成功',
								timeout: 1300,
								showType: 'slide',
								style: {
									right: '',
									bottom: ''
								}
							});
						} else {
							$.messager.alert('删除失败', json.message, 'error');
						}
					}
				});
			}
		});
		AddRow();
	} else {
		$.messager.alert('提醒', "没有选中行", 'info');
	}
}

function getCtime(){
    var curr_time = new Date();
	var strDate = curr_time.getFullYear() + "-";
	strDate += curr_time.getMonth() + 1 + "-";
	strDate += curr_time.getDate()+ " ";
	strDate += curr_time.getHours() + ":";
	strDate += curr_time.getMinutes() + ":";
	strDate += curr_time.getSeconds();
	return strDate;
}
